package com.zyp.boot02.controller;

import com.zyp.boot02.entity.Product;
import com.zyp.boot02.util.DBUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.util.ArrayList;

@RestController
public class ProductController {

    @RequestMapping("/insert")
    public String insert(Product product){
        System.out.println("product = " + product);

        try (
                Connection connection = DBUtil.getConnection();
        ){

            String sql = "INSERT INTO product (id,title,price,num) VALUES (NULL,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, product.getTitle());
            ps.setDouble(2,product.getPrice());
            ps.setInt(3,product.getNum());

            ps.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "添加完成";
    }

    @RequestMapping("/select")
    public String select(){
        ArrayList<Product> list = new ArrayList<>();
        try (
                Connection connection = DBUtil.getConnection();
        ){
            String sql = "SELECT id,title,price,num FROM product";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()){
                Product product = new Product();
                product.setId(rs.getInt("id"));
                product.setTitle(rs.getString("title"));
                product.setPrice(rs.getDouble("price"));
                product.setNum(rs.getInt("num"));
                list.add(product);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        // 把查询到的数据装进一个表格标签里
        String html = "<table border=1>";
        html+="<caption>商品列表</caption>";
        html+="<tr><th>id</th><th>标题</th><th>价格</th><th>库存</th><th>操作</th></tr>";
        for (Product p : list) {
            html += "<tr>";
            html += "<td>"+p.getId()+"</td>";
            html += "<td>"+p.getTitle()+"</td>";
            html += "<td>"+p.getPrice()+"</td>";
            html += "<td>"+p.getNum()+"</td>";
            html += "<td><a href='/delete?id="+p.getId()+"'>删除</a></td>";
            html += "</tr>";
        }
        html+="</table>";
        return html;
    }
    @RequestMapping("/delete")
    public String delete(int id){
        System.out.println("id = " + id);

        try (
                Connection connection = DBUtil.getConnection();
        ){
           String sql = "DELETE FROM product WHERE id=?";
           PreparedStatement ps = connection.prepareStatement(sql);
           ps.setInt(1,id);
           ps.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "删除成功！<a href='/select'>返回列表页面</a>";
    }

    @RequestMapping("/update")
    public String update(Product product){
        try (
                Connection connection = DBUtil.getConnection();
        ){
            String sql = "UPDATE product SET title=?,price=?,num=? WHERE id=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,product.getTitle());
            ps.setDouble(2,product.getPrice());
            ps.setInt(3,product.getNum());
            ps.setInt(4,product.getId());

            ps.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "修改完成！<a href='/select'>返回列表页面</a>";
    }

}
